package cate.game.play.skill.passive.awaken;

import cate.common.table.d.GDAttr;
import cate.common.util.XT;
import cate.game.attr.FightAttr;
import cate.game.play.base.TargetFilterParam;
import cate.game.play.base.TargetSelector;
import cate.game.play.buff.Buff;
import cate.game.play.buff.BuffParam;
import cate.game.play.buff.h.护盾BH;
import cate.game.play.config.PlayCfgArgs;
import cate.game.play.fighter.Fighter;
import cate.game.play.proce.action.ctx.ActionCtx;
import cate.game.play.proce.action.ctx.SkillActionCtx;
import cate.game.play.proce.action.ctx.TimeActionCtx;
import cate.game.play.proce.move.MoveCtx;
import cate.game.play.skill.passive.PassiveHandler;

import java.util.List;

/**
 * 效果2：觉醒状态吸血溢出的生命值会转化为不超过最大生命值25%的【灵能护盾】,
 *       下回合【灵能护盾】破碎并将盾值的10%转化为自身攻击力,持续到战斗结束,吸血转化效果每回合至多触发1次,攻击转化效果每场战斗至多触发7次
 * 效果3：【觉醒·贪婪】额外提升10%伤害加深和5%法术吸血;觉醒状态下回合开始前各有30%概率偷取2个目标25%觉醒值
 * 效果4：【灵能护盾】转化效率提升至15%,且至少转化1.2%最大生命值的攻击力;
 * 效果5：觉醒状态下攻击时有50%概率为1个敌方单位附加【虹吸果蝇】2回合(不可净化),
 *
 */
public class 虹吸者的贪婪PH extends PassiveHandler {

	private double 护盾最大系数;
	private int 护盾buff;
	private double 攻击系数;
	private double 攻击最低系数;
	private int 最大次数;

	private int 偷取概率;
	private int 偷取目标;
	private double 偷取值;

	private int 攻击buff;
	private int 攻击buff概率;

	//护盾最大系数=2500&护盾buff=？？&攻击系数=1000&攻击最低系数=120&最大次数=7
	// &偷取概率=3000&偷取目标=2&偷取值=25
	// &攻击buff=？？&攻击buff概率=？？
	@Override
	public void parse(String code) {
		PlayCfgArgs args = new PlayCfgArgs(code);
		护盾最大系数 = args.getWDouble("护盾最大系数", 0d);
		护盾buff = args.getInt("护盾buff", 0);
		攻击系数 = args.getWDouble("攻击系数", 0d);
		攻击最低系数 = args.getWDouble("攻击最低系数", 0d);
		最大次数 = args.getInt("最大次数", 0);

		偷取概率 = args.getInt("偷取概率",0);
		偷取目标 = args.getInt("偷取目标",0);
		偷取值 = args.getDouble("偷取值",0d);

		攻击buff = args.getInt("攻击buff", 0);
		攻击buff概率 = args.getInt("攻击buff概率", 0);

	}

	private int buffPid;

	@Override
	public void onOverFlowAttrSuck(SkillActionCtx action, long value) {
		if (buffPid > 0) {
			return;
		}
		double max = skill.owner.attr.total().hpTotal() * 护盾最大系数;
		double shieldValue = Math.min(value, max);
		BuffParam param = new BuffParam().buffHpSet(shieldValue);
		buffPid = skill.owner.buff.tryAddByTid(action, skill.owner, 护盾buff, param);
	}


	private int addAttrNum;

	@Override
	public void onRoundBegin(ActionCtx action) {
		if (buffPid <= 0) {
			return;
		}
		Buff shieldBuff = skill.owner.buff.findOneByPid(buffPid);
		if (shieldBuff == null) {
			return;
		}
		shieldBuff.markInvalid(action);
		if (addAttrNum >= 最大次数) {
			return;
		}
		if (shieldBuff.handler instanceof 护盾BH) {
			long shieldValue = ((护盾BH) shieldBuff.handler).shieldValue();
			if (shieldValue <= 0) {
				return;
			}
			addAttrNum++;
			double attValue = shieldValue * 攻击系数;
			if (攻击最低系数 > 0) {
				double attrMin = skill.owner.attr.total().hpTotal() * 攻击最低系数;
				attValue = Math.max(attrMin, attValue);
			}
			skill.owner.attr.addAttr(new FightAttr(GDAttr.ATT, attValue));
		}
	}

	@Override
	public void beforeRoundBegin(ActionCtx action) {
		if (skill.owner.isDead() || !skill.owner.status.isAwaken()) {
			return;
		}
		if (偷取概率 <= 0) {
			return;
		}
		List<Fighter> targets = TargetSelector.doFind(skill.owner, action.getTeamVs(), new TargetFilterParam().targetNumSet(偷取目标));
		for (Fighter target : targets) {
			if (XT.chance(偷取概率)) {
				target.skill.addAwakenEnergy(action, -偷取值);
				skill.owner.skill.addAwakenEnergy(action, 偷取值);
			}
		}
	}

	private boolean addBuff;

	@Override
	public void afterOneAttack(SkillActionCtx action, long totalDamage, boolean strike, Fighter target) {
		if (addBuff) {
			return;
		}
		if (攻击buff <= 0 || 攻击buff概率 <= 0) {
			return;
		}
		if (skill.owner.isDead() || !skill.owner.status.isAwaken()) {
			return;
		}
		Fighter buffTarget = TargetSelector.findOne(skill.owner, action.getTeamVs(), new TargetFilterParam());
		if (buffTarget != null) {
			int pid = buffTarget.buff.tryAddByTid(action, skill.owner, 攻击buff, 攻击buff概率);
			if (pid > 0) {
				addBuff = true;
			}
		}
	}

	@Override
	public void onOwnerMoveDone(MoveCtx move, TimeActionCtx action) {
		addBuff = false;
	}
}
